unit fUserManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, VirtualTrees, SpTBXDkPanels, uDAScriptingProvider, uDADataTable,
  uDAMemDataTable, fClientDataModule, TB2Item, SpTBXItem, Menus, ExtCtrls,
  cxControls, cxSplitter, cxGraphics, cxCustomData, cxStyles, cxTL,
  cxInplaceContainer, cxTLData, cxDBTL, ComCtrls, dxtree, dxdbtree, cxMaskEdit;

type  TUserType = (utUser, utDepartment);

type
  TUserNodeData = record
    UserType: TUserType;
    UserName: WideString;
    UserData: TObject;
  end;
  PUserNodeData = ^TUserNodeData;

type
  TfmUserManage = class(TForm)
    tblDepartment: TDAMemDataTable;
    SpTBXPopupMenu1: TSpTBXPopupMenu;
    SpTBXItem1: TSpTBXItem;
    SpTBXItem2: TSpTBXItem;
    cxSplitter1: TcxSplitter;
    pnlMain: TPanel;
    vstUser: TVirtualStringTree;
    tblUser: TDAMemDataTable;
    procedure FormCreate(Sender: TObject);
    procedure SpTBXItem2Click(Sender: TObject);
    procedure vstUserGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
    procedure vstUserFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex);
    procedure vstUserGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean;
      var ImageIndex: Integer);
  private
    { Private declarations }
    function GetParentNode(AText: WideString): PVirtualNode;
  public
    { Public declarations }
  end;

var
  fmUserManage: TfmUserManage;

implementation

uses
  fDepartment, fUser;

{$R *.dfm}

procedure ShowEmbedForm(AFormClass: TFormClass; AOwner: TComponent; var AForm: TForm);
begin
  if AForm = nil then
  begin
    AForm := AFormClass.Create(AOwner);
    AForm.Align := alClient;
    AForm.BorderStyle := bsNone;
    AForm.Parent := TWinControl(AOwner);
    AForm.Visible := True;
  end
  else
  begin
    AForm.Visible := True;
  end;
end;

function TfmUserManage.GetParentNode(AText: WideString): PVirtualNode;
var
  node: PVirtualNode;
  nd: PUserNodeData;
begin
  Result := nil;
  node := vstUser.GetFirst;
  while not (node = nil) do
  begin
    nd := vstUser.GetNodeData(node);
    if nd^.UserName = AText then
    begin
      Result := node;
      Exit;    
    end;
    node := vstUser.GetNext(node);
  end;
end;

procedure TfmUserManage.FormCreate(Sender: TObject);
var
  afterscroll: TDADataTableNotifyEvent;
  node: PVirtualNode;
  nd: PUserNodeData;
begin
  vstUser.NodeDataSize := SizeOf(TUserNodeData);
  tblDepartment.Open;
  if tblDepartment.IsEmpty then
    Exit;
  tblDepartment.First;
  while not tblDepartment.EOF do
  begin
    node := vstUser.AddChild(nil);
    nd := vstUser.GetNodeData(node);
    nd.UserName := tblDepartment.FieldByName('bd_name').AsWideString;
    nd.UserType := utDepartment;
    tblDepartment.Next;
  end;

  tblUser.Open;
  if tblUser.IsEmpty then
    Exit;
  tblUser.First;
  while not tblUser.EOF do
  begin
    node := GetParentNode(tblUser.FieldByName('bu_depart').AsWideString);
    node := vstUser.AddChild(node);
    nd := vstUser.GetNodeData(node);
    nd.UserName := tblUser.FieldByName('bu_name').AsWideString;
    nd.UserType := utUser;
    tblUser.Next;
  end;
end;

procedure TfmUserManage.SpTBXItem2Click(Sender: TObject);
var
  nd: PUserNodeData;
begin
  ShowEmbedForm(TfmDepartment,  pnlMain, TForm(fmDepartment));
  nd := vstUser.GetNodeData(vstUser.FocusedNode);
  fmDepartment.m_name := nd^.UserName;
  fmDepartment.ShowData;
end;

procedure TfmUserManage.vstUserFocusChanged(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex);
var
  nd: PUserNodeData;
begin
  nd := vstUser.GetNodeData(vstUser.FocusedNode);
  if nd^.UserType = utDepartment then
  begin
    ShowEmbedForm(TfmDepartment,  pnlMain, TForm(fmDepartment));
    fmDepartment.m_name := nd^.UserName;
    fmDepartment.ShowData;
  end
  else
  begin
    ShowEmbedForm(TfmUser,  pnlMain, TForm(fmUser));
//    fmUser.m_name := nd^.UserName;
//    fmUser.ShowData;
  end;

end;

procedure TfmUserManage.vstUserGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
begin
//
end;

procedure TfmUserManage.vstUserGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);
var
  nd: PUserNodeData;
begin
  nd := Sender.GetNodeData(Node);
  CellText := nd^.UserName;
end;

end.
